#include <iostream>
#include <thread>
#include <atomic>
#include <Eigen/Dense>

using namespace std;
using namespace Eigen;

atomic<Matrix<double, 10, 1> *> ato_Mat1;
atomic<Matrix<double, 10, 1> *> ato_Mat2;

// void thread1()
// {
//     Matrix<double, 10, 1>* Mat1 = new Matrix<double, 10, 1>();
//     *Mat1 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;
//     ato_Mat1.store(Mat1);
//     cout << "Thread1: " << *ato_Mat1.load() << endl;
// }

void thread1()
{
    Matrix<double, 10, 1> *Mat1 = new Matrix<double, 10, 1>();
    *Mat1 << 1, 2, 3, 4, 5, 6, 7, 8, 9, 10;
    ato_Mat1.store(Mat1);
    cout << "Thread1: " << *ato_Mat1.load() << endl;
}
void thread2()
{
    Matrix<double, 10, 1> *Mat2 = new Matrix<double, 10, 1>();
    *Mat2 << 11, 12, 13, 14, 15, 16, 17, 18, 19, 20;
    ato_Mat2.store(Mat2);

    cout << "Thread2: " << *ato_Mat2.load() << endl;
}

int main()
{
    thread threadmat1(thread1);
    thread threadmat2(thread2);

    threadmat1.join();
    threadmat2.join();

    cout << "Main: " << *ato_Mat1.load() << " " << *ato_Mat2.load() << endl;

    // 释放内存
    delete ato_Mat1.load();
    delete ato_Mat2.load();

    return 0;
}
